//
//  AppsAnimation.m
//  NineAsk
//
//  Created by Vescky on 12-11-9.
//  Copyright (c) 2012年 Apps123. All rights reserved.
//

#import "AppsAnimation.h"
#import <QuartzCore/QuartzCore.h>

@implementation AppsAnimation

+(void)scaleBounceAnimationIn:(UIView*)_view fromPoint:(CGPoint)_center
{
    if (_center.x > 0 || _center.y > 0) {
        _view.center = _center;
    }
    
    [_view setTransform:CGAffineTransformScale(CGAffineTransformIdentity, 0.000001, 0.000001)];
    
    [UIView animateWithDuration:.15 animations:^{
        [_view setTransform:CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1)];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:.15 animations:^{
            [_view setTransform:CGAffineTransformScale(CGAffineTransformIdentity, .8, .8)];
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:.15 animations:^{
                [_view setTransform:CGAffineTransformIdentity];
            }];
        }];
    }];
}

+(void)scaleBounceAnimationOut:(UIView*)_view fromPoint:(CGPoint)_center
{
    if (_center.x > 0 || _center.y > 0) {
        _view.center = _center;
    }
    
    [_view setTransform:CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0)];
    
    [UIView animateWithDuration:.15 animations:
     ^{
        [_view setTransform:CGAffineTransformScale(CGAffineTransformIdentity, 0.00001, 0.00001)];
      } completion:^(BOOL finished)
        {
           [_view removeFromSuperview];
    }];
    
    
}

+ (void)pushFromRightAnimation:(UIView*)_view {
    CATransition *transition = [CATransition animation];
    
    transition.duration = 0.3f;
    
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    
    transition.type = kCATransitionMoveIn;
    
    transition.subtype = kCATransitionFromRight;
    
    transition.delegate = self;
    
    [_view.layer addAnimation:transition forKey:nil];
    
}

+ (void)popToLeftAnimation:(UIView*)_view {
    
    CATransition *transition = [CATransition animation];
    
    transition.duration = 0.3f;
    
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    
    transition.type = kCATransitionPush;
    
    transition.subtype = kCATransitionFromRight;
    
    transition.delegate = self;
    
    [_view.layer addAnimation:transition forKey:nil];
    
    [_view removeFromSuperview];
}

+ (void)fadeAwayAnimation:(UIView*)_view {
    CATransition *transition = [CATransition animation];
    
    transition.duration = 0.3f;
    
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    
    transition.type = kCATransitionFade;
    
    transition.delegate = self;
    
    [_view.layer addAnimation:transition forKey:nil];
    
}

+(void)exChangeOut:(UIView *)changeOutView dur:(CFTimeInterval)dur{
    
    CAKeyframeAnimation * animation;
    animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    
    animation.duration = dur;
    
    //animation.delegate = self;
    
    animation.removedOnCompletion = NO;
    
    animation.fillMode = kCAFillModeForwards;
    
    NSMutableArray *values = [NSMutableArray array];
    
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
    
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)]];
    
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 0.9)]];
    
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    
    animation.values = values;
    
    animation.timingFunction = [CAMediaTimingFunction functionWithName: @"easeInEaseOut"];
    
    [changeOutView.layer addAnimation:animation forKey:nil];
    
}

@end
